package android.slc.fast.vd.list

import cn.hutool.core.collection.CollUtil
import cn.hutool.core.collection.CollectionUtil
import javafx.scene.Node
import javafx.scene.layout.ColumnConstraints
import javafx.scene.layout.GridPane
import javafx.scene.layout.Priority
import java.io.Serializable

/**
 * @author slc
 * @date 2024/12/5
 **/
interface BaseGridViewVdIface<T : Serializable> {

    companion object {
        val GRID_DIRECTION_V = 0
        val GRID_DIRECTION_H = 1
    }

    fun getGridPane(): GridPane

    fun getScrollPaneId(): String {
        return "#scrollPane"
    }

    fun getGridPaneId(): String {
        return "#gridPane"
    }

    fun getDirectionCount(): Int {
        return 5
    }

    fun getDirection(): Int {
        return GRID_DIRECTION_V
    }

    fun loadItemBefore(dataList: List<T>) {
        this.getGridPane().children.clear()
        val currentDirectionCount = getDirectionCount()
        for (index in 0 until currentDirectionCount) {
            if (getDirection() == GRID_DIRECTION_V) {
                //什么也不做
            } else {
                //什么也不做
            }
            //目前横向纵向一样的
            val columnConstraints = ColumnConstraints()
            columnConstraints.hgrow = Priority.ALWAYS
            this.getGridPane().columnConstraints.add(columnConstraints)
        }
    }

    fun loadItem(dataList: List<T>) {
        val currentDirectionCount = getDirectionCount()
        var rowIndex = 0
        CollectionUtil.forEach(dataList, object : CollUtil.Consumer<T> {
            private var columnIndex: Int = 0

            override fun accept(value: T, index: Int) {
                if (index != 0) {
                    this.columnIndex = index % currentDirectionCount
                    if (this.columnIndex == 0) {
                        rowIndex++
                    }
                }
                val itemView = getItemView(value)
                getGridPane().add(itemView, this.columnIndex, rowIndex)
            }
        })
        val pageSize = dataList.size
        val emptyCount = if (pageSize <= currentDirectionCount) {
            currentDirectionCount - pageSize
        } else {
            currentDirectionCount - pageSize % currentDirectionCount
        }
        for (index in 0 until emptyCount) {
            val itemView = getPlaceholderItemView()
            itemView?.let {
                val columnIndex = emptyCount + 1 + index
                this.getGridPane().add(itemView, columnIndex, rowIndex)
            }
        }
    }

    fun getItemView(item: T): Node

    fun getPlaceholderItemView(): Node? {
        return null
    }
}